<template>
    <div>
        <Row :gutter="24">
            <Col span="12">
                <Form :model="formItem" :label-width="118">
                    <FormItem label="发病时间">
                        <DatePicker v-model="formItem.sick_time_str" style="width: 100%" type="date" placeholder="请选择发病时间" ></DatePicker>
                    </FormItem>
                    <FormItem label="主诉">
                        <Input v-model="formItem.main_desc" type="textarea" placeholder="请输入" />
                    </FormItem>
                    <FormItem label="既往史">
                        <Input v-model="formItem.jw_history" type="textarea" placeholder="请输入" />
                    </FormItem>
                    <FormItem label="现病史">
                        <Input v-model="formItem.now_medical_history" type="textarea" placeholder="请输入" />
                    </FormItem>
                    <FormItem label="个人史">
                        <Input v-model="formItem.person_history" type="textarea" placeholder="请输入" />
                    </FormItem>
                    <FormItem label="家族史">
                        <Input v-model="formItem.family_history" type="textarea" placeholder="请输入" />
                    </FormItem>
                    <FormItem label="体格检查">
                        <Input v-model="formItem.physique_history" placeholder="请输入" clearable/>
                    </FormItem>
                    <FormItem label="过敏史">
                        <Input v-model="formItem.allergy_history" placeholder="请输入" clearable/>
                    </FormItem>
                    <FormItem label="辅助检查结果">
                        <Input v-model="formItem.auxiliary_result" placeholder="请输入" clearable/>
                    </FormItem>
                    <FormItem label="诊断">
                        <Input v-model="formItem.diagnose" placeholder="请输入" clearable/>
                    </FormItem>
                    <FormItem label="治疗意见">
                        <Input v-model="formItem.clinic_opinion" type="textarea" placeholder="请输入" />
                    </FormItem>
                    <FormItem label="病种">
                        <Select v-model="formItem.patient_diseases_id" clearable>
                            <Option v-for="(items, index) in diseaseList" :key="'disease'+index" :value="items.id">{{items.name}}</Option>
                        </Select>
                    </FormItem>
                    <FormItem>
                        <Button type="primary" @click="submitMedicalEdit">保存</Button>
<!--                        <Button @click="resetMedicalInfo" style="margin-left: 8px">重置</Button>-->
                    </FormItem>
                </Form>
            </Col>
            <Col span="12">
                <div>
                    <Timeline class="ivu-pt-16">
                        <TimelineItem v-for="(items, index) in medicalList" :key="'medical' + index">
                            <p class="time">{{items.created_at_str}}</p>
                            <p class="content">{{items.desc}}</p>
                            <p class="consultant">
                                {{items.profession_name}}
                                <span class="c-inr">{{items.operate_name}}</span>
                            </p>
                        </TimelineItem>

                    </Timeline>
                    <Form :model="recordItem">
                        <FormItem>
                            <Input v-model="recordItem.desc" type="textarea" placeholder="请输入门诊记录" />
                        </FormItem>
                        <FormItem>
                            <Button type="primary" @click="submitRecord">提交</Button>
                        </FormItem>
                    </Form>
                </div>
            </Col>
        </Row>
    </div>
</template>
<script>
    import { PatientMedicalList, PatientMedicalInfo, PatientMedicalEdit, PatientMedicalCreate } from '@api/patient'
    import { formatWithYear } from '@/utils/datetime';

    export default {
        name: 'case',
        data () {
            return {
                formItem: {
                    sick_time_str: '',
                    main_desc: '',
                    now_medical_history: '',
                    allergy_history: '',
                    auxiliary_result: '',
                    clinic_opinion: '',
                    patient_diseases_id: '',
                    jw_history: '', // 既往史
                    person_history: '', // 个人史
                    family_history: '', // 家族史
                    physique_history: '', // 体格检查
                    diagnose: '' // 诊断
                },
                recordItem: {
                    desc: ''
                },
                patientId: '',
                medicalList: [],
                medicalInfo: {},
                diseaseList: [],
                clinic: []
            };
        },
        methods: {
            getData (id) {
                this.patientId = id
                this.patientMedicalList()
                this.patientMedicalInfo()
            },
            patientMedicalList () {
                const param = {
                    patient_user_id: this.patientId
                }
                PatientMedicalList(param).then((res) => {
                    this.medicalList = res.list
                }).catch(() => {})
            },
            patientMedicalInfo () {
                const param = {
                    patient_user_id: this.patientId
                }
                PatientMedicalInfo(param).then((res) => {
                    this.medicalInfo = res
                    this.formItem = {
                        sick_time_str: res.sick_time_str,
                        main_desc: res.main_desc,
                        now_medical_history: res.now_medical_history,
                        allergy_history: res.allergy_history,
                        auxiliary_result: res.auxiliary_result,
                        clinic_opinion: res.clinic_opinion,
                        patient_diseases_id: parseInt(res.patient_diseases_id, 10) + '',
                        jw_history: res.jw_history,
                        person_history: res.person_history,
                        family_history: res.family_history,
                        physique_history: res.physique_history,
                        diagnose: res.diagnose
                    }
                    this.diseaseList = res.condition.patient_diseases_list
                }).catch(() => {})
            },
            resetMedicalInfo () {
                const res = this.medicalInfo
                this.formItem = {
                    sick_time_str: res.sick_time_str,
                    main_desc: res.main_desc,
                    now_medical_history: res.now_medical_history,
                    allergy_history: res.allergy_history,
                    auxiliary_result: res.auxiliary_result,
                    clinic_opinion: res.clinic_opinion,
                    patient_diseases_id: parseInt(res.patient_diseases_id, 10) + '',
                    jw_history: res.jw_history,
                    person_history: res.person_history,
                    family_history: res.family_history,
                    physique_history: res.physique_history,
                    diagnose: res.diagnose
                }
            },
            submitRecord () {
                const params = {
                    patient_user_id: this.patientId,
                    desc: this.recordItem.desc
                }
                PatientMedicalCreate(params).then((res) => {
                    // console.log(res)
                    this.$Message.success(' 提交门诊记录成功');
                    this.patientMedicalList();
                    this.recordItem.desc = ''
                }).catch(() => {})
            },
            submitMedicalEdit () {
                const params = {
                    patient_user_id: this.patientId,
                    sick_time: this.formDate(this.formItem.sick_time_str),
                    main_desc: this.formItem.main_desc,
                    now_medical_history: this.formItem.now_medical_history,
                    allergy_history: this.formItem.allergy_history,
                    auxiliary_result: this.formItem.auxiliary_result,
                    clinic_opinion: this.formItem.clinic_opinion,
                    patient_diseases_id: parseInt(this.formItem.patient_diseases_id, 10),
                    jw_history: this.formItem.jw_history,
                    person_history: this.formItem.person_history,
                    family_history: this.formItem.family_history,
                    physique_history: this.formItem.physique_history,
                    diagnose: this.formItem.diagnose
                }
                PatientMedicalEdit(params).then((res) => {
                    // console.log(res)
                    this.$Message.success('保存病例信息成功');
                    this.patientMedicalInfo();
                }).catch(() => {})
            },
            formDate (date) {
                return date ? formatWithYear(date, '-') : ''
            }
        }
    };
</script>
